package com.neusoft.controller;

import com.neusoft.pojo.User;
import com.neusoft.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
/**
 * @author 新
 * 有关用户操作的controller层
 */
@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * @param user 用户信息
	 * @return 登录成功返回主页，否则重回登录页面
	 */
	@RequestMapping("login")
	public String login(User user, HttpSession session) {
		//验证用户是否存在
		User user1 = userService.getUserInfo(user);
		if(user1 == null) {
			return "login";
		}
		session.setAttribute("user", user1);
		return "index";
	}
	
	/**
	 * @param user 用户注册信息
	 * @return 成功跳转至登录页面
	 */
	@RequestMapping("regist")
	public String regist(User user) {
		//注册用户,并验证是否注册成功
		boolean b = userService.regist(user);
		if(b) {
			return "login";
		}else {
			return "regist";
		}
		
	}
}
